package com.nowcoder.community.config;

import com.nowcoder.community.quartz.AlphaJob;
import com.nowcoder.community.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * @author TwfPlayer
 * @date 2023/8/19 14:53
 * @description: 该配置只有在第一次被读取, 初始化到数据库后, quartz会读取数据库的信息
 */

@Configuration
public class QuartzConfig {

    /**
     * 刷新贴子热度任务
     * @return
     */
    @Bean
    public JobDetailFactoryBean PostScoreRefreshJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("PostScoreRefreshJob");//任务名,不可重复
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setDurability(true);//持久化保存
        factoryBean.setRequestsRecovery(true);//可恢复
        return factoryBean;
    }

    /**
     * 刷新贴子热度定时器
     * @return
     */
    @Bean
    public SimpleTriggerFactoryBean PostScoreRefreshJobTrigger(JobDetail PostScoreRefreshJobDetail) {//Trigger依赖于JobDetail
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(PostScoreRefreshJobDetail);
        factoryBean.setName("PostScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        factoryBean.setRepeatInterval(1000*60*5);//频率/时间间隔
        factoryBean.setJobDataMap(new JobDataMap());//存储Job状态
        return factoryBean; //该触发器使用的alphaJob每5分钟秒执行一次
    }


    /**
     * Spring的BeanFactory是IOC容器的顶层接口
     * 而FactoryBean是为了简化Bean的实例化过程
     * 1.通过FactoryBean封装了Bean的实例化过程
     * 2.将FactoryBean装配到Spring容器中
     * 3.将FactoryBean注入给其他的bean后
     * 4.该Bean得到的是FactoryBean管理的对象实例
     */

    /**
     * 配置JobDetail
     *
     * @return
     */
//    @Bean
    public JobDetailFactoryBean alphaJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(AlphaJob.class);
        factoryBean.setName("alphaJob");//任务名,不可重复
        factoryBean.setGroup("alphaJobGroup");
        factoryBean.setDurability(true);//持久化保存
        factoryBean.setRequestsRecovery(true);//可恢复
        return factoryBean;
    }


    /**
     * 配置Trigger（有SimpleTriggerFactoryBean，CronTriggerFactoryBean）
     * SimpleTriggerFactoryBean 简单配置，时间间隔
     * CronTriggerFactoryBean 配置定期任务，如每周，每月等
     *
     * @param alphaJobDetail
     * @return
     */
//    @Bean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) {//Trigger依赖于JobDetail
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);
        factoryBean.setName("alphaTrigger");
        factoryBean.setGroup("alphaTriggerGroup");
        factoryBean.setRepeatInterval(3000);//频率/时间间隔
        factoryBean.setJobDataMap(new JobDataMap());//存储Job状态
        return factoryBean; //该触发器使用的alphaJob每3秒执行一次
    }
}
